<?php


namespace Balance\Model;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Sql;
use Zend\Db\TableGateway\TableGateway;

class BalanceTable
{
    protected $tableGateway;
    protected $adapter;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
        $this->adapter = $tableGateway->getAdapter();
    }

    /**
     * 获取所有收支流水
     * @return array
     */
    public function fetchAll()
    {
//        $adapter = $this->adapter;
//        $sql = new Sql($adapter);
//        $select = new Select();
//        $select->from('pc_balance')
//            ->order('loan_id');
//        $statement = $sql->prepareStatementForSqlObject($select);
//        $resultSet = $statement->execute();
//        return iterator_to_array($resultSet);
        $resultSet = $this->tableGateway->select();
        return $resultSet->toArray();
    }

    /**
     * 获取某贷款的所有收支流水
     * @param $loan_id
     * @return array
     */
    public function getBalanceByLoanId($loan_id)
    {
        $resultSet = $this->tableGateway->select(array('loan_id' => $loan_id));
        return $resultSet->toArray();
    }
}